import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Bitmap;
import flash.display.BitmapData;
import TileModel;
import flash.events.Event;

class GunView extends Sprite
{
  public var model:TileModel;
  public var modelBitmapData:BitmapData;
  public var modelBitmap:Bitmap;
  public var offsetY:Int;
  public var offsetX:Int;
  public function new(inModel:TileModel)
  {
    super();
    model = inModel;
    offsetY=0;offsetX=0;
    modelBitmapData = new BitmapData(64, 64, true, 0);
    modelBitmap = new Bitmap(modelBitmapData);

    this.addChild(modelBitmap);
    model.addEventListener(Event.CHANGE, changeHandler);
    addEventListener(Event.ADDED_TO_STAGE, addedToStagHandler);
  }
  private function addedToStagHandler(event:Event)
  {
    //Draw the object
    
    //Add listeners
    addEventListener
      (Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  }

  private function removedFromStageHandler(event:Event)
  {
    removeEventListener
      (Event.ADDED_TO_STAGE, addedToStagHandler);
    removeEventListener
      (Event.REMOVED_FROM_STAGE, removedFromStageHandler);
  }
  
  private function changeHandler(event:Event)
  {
    modelBitmap.y = offsetY;
    modelBitmap.x = offsetX;
    this.x = model.xPos; this.y = model.yPos;//this.rotation += 10;
    if(this.rotation > 90 || rotation < -90)
      {
      this.scaleY = -1;
      }
   else
      {
      this.scaleY = 1;
      }
  }
}